package com.gemme.gemme.wode;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gemme.gemme.R;
import com.gemme.gemme.fragment.BaseLazyFragment;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.DefineLoadMoreView;
import com.gemme.gemme.view.GuanzhuHeadView;
import com.gemme.gemme.view.GuanzhuItem9View;
import com.gemme.gemme.view.GuanzhuSingleItemView;
import com.gemme.gemme.view.RefreshView;
import com.gemme.gemme.view.SingleShouruView;
import com.gemme.gemme.view.StaggeredDividerItemDecoration;
import com.yanzhenjie.recyclerview.OnItemClickListener;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;


/**
 * Created by sp on 2019/7/18.
 * 首页
 */
public class PlatFormShouRuFragment extends BaseLazyFragment  {

    View inflate;
    Context mContext;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        inflate = inflater.inflate(R.layout.fragment_shouru, container, false);

        mContext = PlatFormShouRuFragment.this.getContext();

        initView(inflate);

        return inflate;
    }


    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(View inflate) {
        initRefresh(inflate);
    }

    private void initData() {
//        http://192.168.1.6:9999/user/capital/coinrecord?current=1&size=10&type=1
        String url = DataManager.BaseUrl +
                ":9999/user/capital/coinrecord?" +
                "current=" + "1" +
                "&size=" + "1000" +
                "&type=1";

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(getContext()));
        DataManager.toServer3(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    ShouruBean shouruBean = JSON.parseObject(response, ShouruBean.class);
                    List<ShouruBean.DataBean.RecordsBean> records = shouruBean.getData().getRecords();

                    LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> hashmap = detailData(records);
                    //设置适配器
                    refreshview.setAdapter(new ShouruAdapter(hashmap));
                    System.out.println("");
                }catch (Exception e){

                }
            }
        });
    }

    private LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> detailData(List<ShouruBean.DataBean.RecordsBean> records) {
        LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> hashMap = new LinkedHashMap<>();
        try {
            for (int i = 0; i < records.size(); i++) {
                ShouruBean.DataBean.RecordsBean bean = records.get(i);
                String createTime = bean.getCreateTime();
                if(!TextUtils.isEmpty(createTime) && createTime.length() >= 10){
                    createTime = createTime.substring(0,10);
                }
                ArrayList<ShouruBean.DataBean.RecordsBean> childList;
                if(!hashMap.containsKey(createTime)){//不包含,新建
                    childList = new ArrayList<>();
                    childList.add(bean);
                    hashMap.put(createTime,childList);
                }else{//包含,取出来添加
                    childList = hashMap.get(createTime);
                    childList.add(bean);
                }

            }
        }catch (Exception e){

        }
        return hashMap;
    }


    RefreshView refreshview;
    private void initRefresh(View inflate) {
        refreshview = inflate.findViewById(R.id.refreshview);
        //设置布局管理器
        refreshview.setLayoutManager(new LinearLayoutManager(mContext));
        refreshview.loadMoreFinish(false, false);
    }


    @Override
    public void onLazyLoad() {

        //yc

//        getData();
    }

    public class ShouruAdapter extends RecyclerView.Adapter<ShouruAdapter.ViewHolder>{


        private LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> hashmap;

        public ShouruAdapter(LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> hashmap) {

            this.hashmap = hashmap;
        }

        @NonNull
        @Override
        public ShouruAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_platform_shouru, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new ShouruAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ShouruAdapter.ViewHolder holder, int position) {
            /*Map.Entry<String, ArrayList<ShouruBean.DataBean.RecordsBean>> dataByIndex = getDataByIndex(hashmap, position);
            holder.tv_date.setText(dataByIndex.getKey());
            ArrayList<ShouruBean.DataBean.RecordsBean> value = dataByIndex.getValue();
            if(value == null)
                return;
            for (int i = 0; i < value.size(); i++) {
                ShouruBean.DataBean.RecordsBean recordsBean = value.get(i);
                SingleShouruView singleShouruView = new SingleShouruView(mContext);
                singleShouruView.setData(recordsBean);
                holder.ll_content.addView(singleShouruView);
            }*/

           try {
               String key = getDataByIndex1(hashmap, position);
               holder.tv_date.setText(key + "");
               ArrayList<ShouruBean.DataBean.RecordsBean> value = hashmap.get(key);
               if(value == null)
                   return;
               for (int i = 0; i < value.size(); i++) {
                   ShouruBean.DataBean.RecordsBean recordsBean = value.get(i);
                   SingleShouruView singleShouruView = new SingleShouruView(mContext);
                   singleShouruView.setData(recordsBean);
                   holder.ll_content.addView(singleShouruView);
               }
           }catch (Exception e){

           }
        }

        public  Map.Entry<String, ArrayList<ShouruBean.DataBean.RecordsBean>> getDataByIndex(LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> map,int index){
            Map.Entry<String, ArrayList<ShouruBean.DataBean.RecordsBean>> next = null;
            try {
                Set<Map.Entry<String, ArrayList<ShouruBean.DataBean.RecordsBean>>> entrySet = map.entrySet();
                Iterator<Map.Entry<String, ArrayList<ShouruBean.DataBean.RecordsBean>>> entryit  = entrySet.iterator();
                int i = 0;
                while(entryit.hasNext()){
                    if (i == index){
                        next = entryit.next();
                        break;
                    }
                    i++;

                }
            }catch (Exception e){

            }

            return next;
        }

        public String getDataByIndex1(LinkedHashMap<String, ArrayList<ShouruBean.DataBean.RecordsBean>> map,int index){

            int i = 0;
            String mapkey = "";
            for (String key : map.keySet()) {
                if(i == index){
                    mapkey = key;
                }
                System.out.println("Key = " + key);
                i ++;
            }

            return mapkey;
        }


        @Override
        public int getItemCount() {
            return hashmap.size();
        }



        public class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_date;
            LinearLayout ll_content;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);

                tv_date = itemView.findViewById(R.id.tv_date);
                ll_content = itemView.findViewById(R.id.ll_content);
            }
        }
    }









}
